﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SC.eWedding.BusinessLogic.Views;
using SC.eWedding.BusinessLogic.Handlers;
using System.Text;

public partial class Administrador_Controles_vdProveedor : System.Web.UI.UserControl
{
    #region Interface

    public delegate void CargarGrillaHandler();
    public event CargarGrillaHandler RecargarGrilla;

    #endregion

    #region Public Properties

    
    string _onClientEdit = "";
    public string OnClientEdit
    {
        set { _onClientEdit = value; }
        get { return _onClientEdit; }
    }

    string _onClientReportar = "";
    public string OnClientReportar
    {
        set { _onClientReportar = value; }
        get { return _onClientReportar; }
    }

    #endregion

    #region Eventos

    protected void gvProveedores_RowEditing(object sender, GridViewEditEventArgs e)
    {
        if (RecargarGrilla != null)
            RecargarGrilla();
    }

    protected void gvProveedores_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvProveedores.PageIndex = e.NewPageIndex;
        gvProveedores.DataBind();

        if (RecargarGrilla != null)
            RecargarGrilla();
    }

    protected void gvProveedores_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (bool.Parse(hidConfirmacion.Value))
        {
            int id = Convert.ToInt32(gvProveedores.DataKeys[e.RowIndex].Values["Id"]);

            try
            {
                ProveedorView prov = ProveedorHandler.FindById(id);
                ProveedorHandler.Delete(prov);

                if (RecargarGrilla != null)
                    RecargarGrilla();
            }
            catch
            {
                if (!Page.ClientScript.IsStartupScriptRegistered("script_Error"))
                {
                    StringBuilder script = new StringBuilder();

                    script.Append("<script language=\"jscript\">\n");
                    script.Append("   alert('Se produjo un error al intentar eliminar el producto');\n");
                    script.Append("</script>\n");

                    System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "script_Error", script.ToString(), false);
                }
            }
        }
    }

    protected void gvProveedores_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int id = Convert.ToInt32(gvProveedores.DataKeys[e.Row.RowIndex].Values["Id"]);
            //ProveedorView pro = ProveedorHandler.FindById(id);

            ImageButton edit = (ImageButton)e.Row.Cells[e.Row.Cells.Count - 1].FindControl("btnEditar");
            edit.Attributes.Add("onclick", String.Format(this.OnClientEdit, id));
            ImageButton borrar = (ImageButton)e.Row.Cells[e.Row.Cells.Count - 1].FindControl("btnBorrar");
            borrar.Attributes.Add("onclick", "javascript: return (document.getElementById('" + hidConfirmacion.ClientID + "').value = confirm('¿ Esta seguro de eliminar el proveedor?'));");

        }
    }

    #endregion

    #region Metodos Publicos

    public void CargarGrilla(List<ProveedorView> proveedores)
    {
        gvProveedores.DataSource = proveedores;
        gvProveedores.DataBind();
    }

    #endregion
}
